﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    public static class ExceptionExtensions
    {
        public static string GetExceptionDetail(this Exception ex)
        {
            var exlist = new List<Exception>();
            while (ex != null)
            {
                exlist.Add(ex);
                ex = ex.InnerException;
            }
            exlist.Reverse();
            var str = new StringBuilder();
            foreach (var e in exlist)
            {
                str.AppendFormat("Message:  {0}\r\nSource:  {1}\r\nTargetSite:  {2}\r\nStackTrace:\r\n{3}\r\n",
                    e.Message, e.Source, e.TargetSite, e.StackTrace);
            }
            return str.ToString();
        }
    }
}
